<template>
  <div class="list">
    <el-input placeholder="请输入内容" v-model="formData.key" class="input-with-select">
      <el-button slot="append" icon="el-icon-search" @click.prevent="handleSearch"></el-button>
    </el-input>
    <!-- <Title :name="货 品列表" :size="20" :icon="el-icon-d-arrow-right " /> -->
    <el-table
      stripe
      height="600"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="item_code"
        label="商品编码">
      </el-table-column>
      <el-table-column
        prop="repertory"
        label="货架编号">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="formData.total"
      :current-page.sync="formData.page"
      @current-change="handleCurrentChange"
      >
    </el-pagination>
  </div>
</template>

<script>
import DB from '../axios/db'
export default {
  name: 'list',
  data () {
    return {
      tableData: [
        {
          id: '',
          item_code: '', // 商品编码
          name: '', // 商品名称
          repertory: '' // 货架编号
        }
      ],
      formData: {
        key: '', // 搜索内容
        page: 1, // 当前页
        total: 0 // 总条数
      }
    }
  },
  created () {
    this.$Variable.userLogin()
    this.getTools() // 获取货品列表
  },
  methods: {
    // 获取货品列表
    getTools () {
      const _this = this
      DB.getTools(_this.formData).then(res => {
        // console.log(res)
        _this.tableData = res.data.data
        _this.formData.page = res.data.current_page
        _this.formData.total = res.data.total
      })
    },
    // 搜索
    handleSearch () {
      this.formData.page = 1
      this.getTools() // 获取货品列表
    },
    // 删除单行
    handleClick (val) {
      // console.log(val)
      const _this = this
      DB.delTools({id: val.id}).then(res => {
        // console.log(res)
        if (res.code === 0) {
          _this.$message({
            message: res.message || '删除成功',
            type: 'success'
          })
          this.getTools() // 更新货品列表
          // _this.tableData.filter(item => {
          //   return item !== val.id
          // })
        } else {
          _this.$message({
            message: res.message || '删除失败',
            type: 'error'
          })
        }
      })
    },
    // 翻页
    handleCurrentChange (val) {
      this.getTools() // 获取货品列表
    }
  }
}
</script>

<style scoped lang="scss">
.list {
  .el-pagination {
    text-align: right;
    padding-top: 20px;
  }
}
</style>
